<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="题目内容" prop="content">
        <el-input v-model="queryParams.content" type="text" placeholder="请输入题目内容" />
      </el-form-item>
      <el-form-item label="题目类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择题目类型" clearable size="small">
          <el-option 
                v-for="dict in appraiseQueTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:appraiseQue:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:appraiseQue:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:appraiseQue:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:appraiseQue:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appraiseQueList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="题目类型" :formatter="appraiseQueTypeFormat" align="center" prop="type" />
      <el-table-column label="状态" :formatter="statusFormat" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:appraiseQue:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:appraiseQue:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教学评价题库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-tabs v-model="activeName" @tab-click="handleTabChange">
          <el-tab-pane label="基本信息" name="first">
            <el-form-item label="内容">
              <editor v-model="form.content" :min-height="192"/>
            </el-form-item>
            <el-form-item label="题目类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择题目类型">
                <el-option 
                    v-for="dict in appraiseQueTypeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue" />
              </el-select>
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in statusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                  >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="分值选项" name="second">
            <el-button size="mini" type="primary"
              icon="el-icon-plus" @click="addAppraiseQueOptions">新增分值选项</el-button>
            <div style="margin: 15px 0;"></div>
            <el-row  style="margin-left: -115px;" v-for="(item,index) in queOptionsInfo" :key="index">
              <el-col :span="6">
                <el-form-item label="">
                  <el-input style="width:200px"  v-model="item.name" placeholder="请输入选项名" >
                    <template slot="prepend">选项名</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="">
                  <el-input style="width:200px"  v-model="item.content" placeholder="请输入内容" >
                    <template slot="prepend">内容</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="">
                  <el-input style="width:200px"  v-model="item.score" placeholder="请输入选项分值" >
                    <template slot="prepend">分值</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                   <div style="text-align:center;">
                    <el-button size="mini" type="danger"
                      icon="el-icon-delete" v-if="queOptionsInfo.length > 1" 
                      @click="delAppraiseQueOptions(index)">删除</el-button>
                   </div>
              </el-col>
            </el-row>

          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppraiseQue, getAppraiseQue, delAppraiseQue, addAppraiseQue, updateAppraiseQue, exportAppraiseQue } from "@/api/edu/appraiseQue";
import Editor from '@/components/Editor';

export default {
  name: "AppraiseQue",
  components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学评价题库表格数据
      appraiseQueList: [],
      statusOptions: [],
      appraiseQueTypeOptions: [],
      // 弹出层标题
      title: "",
      activeName: 'first',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        status: null,
        type: null
      },
      // 评价题库选项
      queOptionsInfo: [{
        name: null,
        content: null,
        score: null,
      }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        /*
        type: [
          { required: true, message: "题目类型不能为空", trigger: "change" }
        ]
        */
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("edu_appraise_que_type").then(response => {
      this.appraiseQueTypeOptions = response.data;
    });
  },
  methods: {
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    appraiseQueTypeFormat(row, column) {
      return this.selectDictLabel(this.appraiseQueTypeOptions, row.type);
    },
    /** 查询教学评价题库列表 */
    getList() {
      this.loading = true;
      listAppraiseQue(this.queryParams).then(response => {
        this.appraiseQueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleTabChange() {
      if("second" === this.activeName) {
        if(this.form.classAppraiseQueOptionsList.length > 0 ) {
          this.queOptionsInfo = []//.splice(0,1) // 删除原来的空行
          this.form.classAppraiseQueOptionsList.forEach(option => {
            this.queOptionsInfo.push({
              name: option.name,
              content: option.content,
              score: option.score,
            })
          })
        }
        
      } else {
        // 切换tab时需要临时保存数据
        if(this.queOptionsInfo.length > 0) {
          this.form.classAppraiseQueOptionsList = []
          this.queOptionsInfo.forEach(option =>{
            this.form.classAppraiseQueOptionsList.push({
                name: option.name,
                content: option.content,
                score: option.score,
              })
          })
        }
      }
    },
    // 增加教学评价题库选项
    addAppraiseQueOptions() {
      this.queOptionsInfo.push({
        name: null,
        content: null,
        score: null,
      })
    },
    // 删除教学评价题库选项
    delAppraiseQueOptions(index) {
      if(this.queOptionsInfo.length > 1) {
        this.queOptionsInfo.splice(index,1)
      } else {
        this.msgError("至少有一个评价题库选项");
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        queId: null,
        content: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        type: '0',
        classAppraiseQueOptionsList: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.queId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教学评价题库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const queId = row.queId || this.ids
      getAppraiseQue(queId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教学评价题库";
      });
      this.activeName = 'first'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.classAppraiseQueOptionsList = []
          this.queOptionsInfo.forEach(option =>{
            this.form.classAppraiseQueOptionsList.push({
                name: option.name,
                content: option.content,
                score: option.score,
              })
          })

          if (this.form.queId != null) {
            updateAppraiseQue(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppraiseQue(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const queIds = row.queId || this.ids;
      this.$confirm('是否确认删除教学评价题库编号为"' + queIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAppraiseQue(queIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教学评价题库数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAppraiseQue(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
